<?php            
    $today = strtolower(date("l"));
    $schedule = array();
    $workouts = array(
        "monday"    => array(),
        "tuesday"   => array(),
        "wednesday" => array(),
        "thursday"  => array(),
        "friday"    => array(),
        "saturday"  => array(),
        "sunday"    => array()
    );
    
    $now        = arrGet($_POST,"now");    
    $startdate  = arrGet($_POST,"startdate");
    
    # Aseta päivämääräväli
    $tmp = strtotime($startdate);
    $enddate    = date("Y-m-d",strtotime("+6 days",$tmp));        
    $minus4w    = date("Y-m-d",strtotime("-28 days",$tmp));
    $lastmonday = date("Y-m-d",strtotime("-7 days",$tmp));
    $nextmonday = date("Y-m-d",strtotime("+7 days",$tmp));
    $plus4w     = date("Y-m-d",strtotime("+28 days",$tmp));
    
    # Hae harjoituspohjat
    $schedule = API::trainingProgramSchedule(array(
        "startdate" => $startdate,
        "enddate"   => $enddate
    ));
    
    # Saatiinko hakutuloksia    
    $list = arrGet($schedule,"list");
    if (isset($tmp) && !empty($list)) {
        $weekdays = array(
            "monday"    => $tmp,
            "tuesday"   => strtotime("+1 days",$tmp),
            "wednesday" => strtotime("+2 days",$tmp),
            "thursday"  => strtotime("+3 days",$tmp),
            "friday"    => strtotime("+4 days",$tmp),
            "saturday"  => strtotime("+5 days",$tmp),
            "sunday"    => strtotime("+6 days",$tmp)
        );
        
        # Käy hakutulokset läpi
        foreach ($list as $row) {
            foreach ($weekdays as $day => $date) {
                # Tarkasta osuuko päivämäärä oikein                
                if ($date != strtotime($row["workout_date"])) {
                    continue;
                }
                
                $workouts[$day][] = $row;
            }
        }
    }
?>

<div id="tpcalendar-div-weekcal">
    <table class="table">
        <thead>        
            <tr>
                <?php
                    # Tulosta päivämäärien nimet ruudulle
                    foreach ($workouts as $day => $arr) {
                        $endian = "";
                        if ($now == $startdate && $day == $today) {
                            $endian .= "&nbsp;*";
                        }
                        
                        echo "<th>".
                            mb_strtoupper($day).
                            $endian.
                            "</th>";
                    }
                ?>
            </tr>
        </thead>
        
        <tbody>
            <tr>
                <?php
                    # Tulosta harjoitepohjat ruudulle
                    foreach ($workouts as $arr) {
                        echo "<td>";
                        if (empty($arr)) {
                            echo "&nbsp;</td>";
                            continue;
                        }
                        
                        $i = 0;
                        $prevId = null;
                        foreach ($arr as $row) {
                            if ($i > 0) {
                                echo "<br />";                            
                            }
                            
                            $tpHashId = htmlescape($row["hashid"]);
                            if ($prevId != $row["hashid"]) {
                                $name = htmlescape($row["program_name"]);
                                
                                echo <<<HTML
                                    <a
                                      class="a-subview-dialog"
                                      data-ui-view="tplist"
                                      data-ui-subview="info"
                                      data-ui-title="$name"
                                      data-hashid="$tpHashId"
                                      href="#">
                                        <strong>$name</strong>
                                    </a><br />
HTML;
                            }
                            
                            $hashid = htmlescape($row["templatehashid"]);
                            $extratext = htmlescape($row["template_extratext"]);                        
                            $workoutDate = htmlescape($row["workout_date"]);
                            
                            echo <<<HTML
                                <a
                                  class="a-subview-dialog margin-left"
                                  data-ui-view="templatelist"
                                  data-ui-subview="info"
                                  data-ui-title="$extratext"
                                  data-hashid="$hashid"
                                  data-training-program-hashid="$tpHashId"
                                  data-workout-date="$workoutDate"
                                  href="#">
                                    *&nbsp;$extratext
                                </a>
HTML;
                            
                            $prevId = $row["hashid"];
                            $i++;
                        }
                        
                        echo "</td>";
                    }
                ?>
            </tr>
        </tbody>        
    </table>
</div>

<hr />

<div>
    <strong>Week:</strong>        
    <?php
        echo ltrim(date("W",strtotime($startdate)),"0").
            "&nbsp;(".
            $startdate.
            "&nbsp;to&nbsp;".
            $enddate.
            ")";
    ?>
</div>

<?php    
    # Disabloidaanko Now painike
    $aClass = " class=\"tpcalendar-a-navigation\"";
    $liClass = "";
    if ($now == $startdate) {
        $aClass = "";
        $liClass = " class=\"disabled\"";
    }
?>

<nav>
    <ul class="pagination">
        <li>
            <a
              class="tpcalendar-a-navigation"
              href="#"
              title="Minus four weeks"
              data-startdate="<?php echo htmlescape($minus4w); ?>">
                <span aria-hidden="true">&laquo;&laquo;</span>
                <span class="sr-only">Minus four weeks</span>
            </a>
        </li>
        
        <li>
            <a
              class="tpcalendar-a-navigation"
              href="#"
              title="Last week"
              data-startdate="<?php echo htmlescape($lastmonday); ?>">
                <span aria-hidden="true">&laquo;</span>
                <span class="sr-only">Last week</span>
            </a>
        </li>
        
        <li<?php echo $liClass; ?>>
            <a<?php echo $aClass; ?>
              href="#"
              data-startdate="<?php echo htmlescape($now); ?>">
                Now
            </a>
        </li>
        
        <li>
            <a
              class="tpcalendar-a-navigation"
              href="#"
              title="Next week"
              data-startdate="<?php echo htmlescape($nextmonday); ?>">
                <span aria-hidden="true">&raquo;</span>
                <span class="sr-only">Next week</span>
            </a>
        </li>
        
        <li>
            <a
              class="tpcalendar-a-navigation"
              href="#"
              title="Plus four weeks"
              data-startdate="<?php echo htmlescape($plus4w); ?>">
                <span aria-hidden="true">&raquo;&raquo;</span>
                <span class="sr-only">Plus four weeks</span>
            </a>
        </li>
    </ul>
</nav>